package com.hotel.front;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.POSTaxLogic;

import data.POSTax;
import data.passingData.ResultData;
@SuppressWarnings("serial")
public class POSTaxService extends HttpServlet{
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("getAllPOSTax")){
				getAllPOSTax(req,resp);
			}else if(doClass.equals("addPOSTax")){
				AddPOSTax(req,resp);
			}else if(doClass.equals("delPOSTax")){
				DeletePOSTax(req,resp);
			}else if(doClass.equals("editPOSTax")){
				EditPOSTax(req,resp);
			}else if(doClass.equals("getPOSTaxById")){
				GetPOSTaxById(req,resp);
			}else if(doClass.equals("editStatusPOSTaxById")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("editSortOrder")){
				EditSortOrder(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	private void AddPOSTax(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String description = req.getParameter("description");
		String title = req.getParameter("title");
		String chargeType = req.getParameter("chargeType");
		Double amount =Double.parseDouble(req.getParameter("amount"));
		Object obj = new POSTaxLogic(req,resp).AddPOSTax(hotelId,title,description,chargeType,amount);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void EditPOSTax(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long unitId = Long.parseLong(req.getParameter("id"));
		String title = req.getParameter("title");
		String description = req.getParameter("description");
		String chargeType = req.getParameter("chargeType");
		Double amount = Double.parseDouble(req.getParameter("amount"));
		Object obj = new POSTaxLogic(req,resp).EditPOSTax(unitId,title,description,chargeType,amount);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long amenId = Long.parseLong(req.getParameter("taxId"));
		Object obj = new POSTaxLogic(req,resp).ChangeStatus(amenId);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void EditSortOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		long sortOrder = Long.parseLong(req.getParameter("sortOrder"));

		Object obj = new POSTaxLogic(req,resp).EditSortOrder(id, sortOrder);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void DeletePOSTax(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String unitIdList = req.getParameter("taxId");
		POSTaxLogic taxLogic = new POSTaxLogic(req,resp);
		for(int i = 0; i < unitIdList.split(",").length ; i++){
			taxLogic.DeletePOSTax(Long.parseLong(unitIdList.split(",")[i]));
		}
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(true));
	}
	
	public void getAllPOSTax(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		Object obj = new POSTaxLogic(req,resp).GetAllPOSTaxLimit(hotelId,offset,line);
		double dataSize = new POSTaxLogic(req,resp).GetAllPOSTax(hotelId).size();
		Gson gson = new Gson();
		List<Object> rsObj = Arrays.asList(dataSize,pointer,line,obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(rsObj));
	}

	private void GetPOSTaxById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		POSTax obj = new POSTaxLogic(req,resp).GetPOSTax(id);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}

}